package ui.views;

import java.text.DecimalFormat;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.nebula.widgets.pshelf.PShelf;
import org.eclipse.nebula.widgets.pshelf.PShelfItem;
import org.eclipse.nebula.widgets.pshelf.RedmondShelfRenderer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.ISelectionListener;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.part.ViewPart;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import ui.Activator;
import ui.control.FixedIncomeInfo;
import ui.control.ProductCommonInfo;
import ui.tempdao.EgnFixedIncomeDao;
import ui.util.CalendarUtil;
import dao.EgnFixedIncome;
import dao.EgnProduct;

public class ProductInfoView extends ViewPart implements ISelectionListener{
	
	public final static Logger logger = LoggerFactory.getLogger(ProductInfoView.class);
	private ProductCommonInfo prodComp;
	private FixedIncomeInfo fixedComp;
	private FixedIncomeInfo fixedComp1;
	
	private DecimalFormat df = new DecimalFormat("#,###.####");
	private DecimalFormat per = new DecimalFormat("#.##%");
	
	private static final Image commonImage= Activator.getImageDescriptor("icons/showcat_co.gif").createImage();
//	final Color blue = Display.getCurrent().getSystemColor(SWT.COLOR_WIDGET_LIGHT_SHADOW);
	final Color white = Display.getCurrent().getSystemColor(SWT.COLOR_WHITE);
	final Color blue = Display.getCurrent().getSystemColor(SWT.COLOR_BLUE);
	
	public void createPartControl(Composite parent) {
		PShelf shelf = new PShelf(parent, SWT.V_SCROLL	);
		shelf.setRenderer(new RedmondShelfRenderer());
//		shelf.setBackground(white);
//		shelf.setForeground(blue);
		
		PShelfItem commonProd = new PShelfItem(shelf, SWT.V_SCROLL);
		commonProd.setText("Common Info");
		commonProd.setImage(commonImage);
		commonProd.getBody().setLayout(new FillLayout());
		
		prodComp = new ProductCommonInfo(commonProd.getBody(), SWT.None);
		
		PShelfItem fixedIncome = new PShelfItem(shelf, SWT.V_SCROLL);
		fixedIncome.setText("FixedIncomeInfo");
		fixedIncome.setImage(commonImage);
		fixedIncome.getBody().setLayout(new FillLayout());
		
		fixedComp = new FixedIncomeInfo(fixedIncome.getBody(), SWT.None	);
		
		PShelfItem future = new PShelfItem(shelf, SWT.None);
		future.setText("Forward/Futures Info");
		
		PShelfItem stOption = new PShelfItem(shelf, SWT.None);
		stOption.setText("Stock Option Info");
		
		PShelfItem irOption = new PShelfItem(shelf, SWT.None);
		irOption.setText("IR Option Info");
		
		
		getSite().getPage().addSelectionListener(this);
	}

	@Override
	public void setFocus() {
		
	}
	
	@Override
	public void selectionChanged(IWorkbenchPart part, ISelection selection) {
		if (selection instanceof IStructuredSelection) {
			Object first = ((IStructuredSelection) selection).getFirstElement();
			if (first instanceof EgnProduct) {
				logger.debug("Part Id :{},{}", part.getTitle(), getSite().getPart().getTitle());
				getSite().getPage().activate(getSite().getPart());
				getSite().getPage().activate(part);
				
				EgnProduct prod = (EgnProduct) first;
				prodComp.getProdId().setText(prod.getProductId());
				prodComp.getProdType().setText(prod.getProductType());
				prodComp.getProdName().setText(prod.getProductName());
				prodComp.getCurrency().setText(prod.getCurrency());
				prodComp.getIssueDate().setText(CalendarUtil.toString(prod.getIssueDate()));
				prodComp.getMatDate().setText(CalendarUtil.toString(prod.getMaturityDate()));
				prodComp.getNotional().setText(df.format(prod.getNotional()));
				prodComp.getCnParty().setText(prod.getCntPartyId());
				prodComp.getCreditGrade().setText(prod.getCreditGrade());
				prodComp.getBizRule().setText(prod.getBizdayRule());
				prodComp.getBizNum().setText(df.format(prod.getBizdayNum()));
				prodComp.getCurrAmt().setText(df.format(prod.getCurrentAmt()));
				prodComp.getDisCurve().setText(prod.getDiscountCurve());
				prodComp.getDisSpread().setText(per.format(prod.getDiscountSpread()));
				prodComp.getCalendar().setText(prod.getCalId());
				
				
				EgnFixedIncome fixedIncome = EgnFixedIncomeDao.getEgnFixedIncome(prod.getProductId());
				
				fixedComp.getAmortMtd().setText(fixedIncome.getAmortizationCd());
				fixedComp.getAmortTerm().setText(fixedIncome.getAmortizationTerm()==null?"null":fixedIncome.getAmortizationTerm());
				fixedComp.getAmortAmt().setText(fixedIncome.getAmortizationAmt()==null?"null":df.format(fixedIncome.getAmortizationAmt()));
				fixedComp.getCpTerm().setText(fixedIncome.getCouponTerm()==null?"null":fixedIncome.getCouponTerm());
				fixedComp.getCpMtd().setText(fixedIncome.getCompoundMtdCd()==null?"null":fixedIncome.getCompoundMtdCd());
				fixedComp.getDcMtd().setText(fixedIncome.getDaycountMtdCd()==null?"null":fixedIncome.getDaycountMtdCd());
				fixedComp.getIsPrepay().setText(fixedIncome.getIsPrepay()==null?"null":fixedIncome.getIsPrepay().toString());
				fixedComp.getIsLoan().setText(fixedIncome.getIsLoan()==null?"null":fixedIncome.getIsLoan().toString());
				fixedComp.getIsEom().setText(fixedIncome.getIsEom()==null?"null":fixedIncome.getIsEom().toString());
				fixedComp.getRefRate().setText(fixedIncome.getRefRateId()==null?"null":fixedIncome.getRefRateId());
				fixedComp.getRfTerm().setText(fixedIncome.getRefixingTerm()==null?"null":fixedIncome.getRefixingTerm());
				fixedComp.getRfRule().setText(fixedIncome.getRefixingRule()==null?"null":fixedIncome.getRefixingRule());
				fixedComp.getCpRate().setText(fixedIncome.getCouponRate()==null?"null":per.format(fixedIncome.getCouponRate()));
				
				
			}
			
		}
	}


}
